package com.woshua.cameramodel;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import com.tbruyelle.rxpermissions2.RxPermissions;

import io.reactivex.functions.Consumer;

public class MainActivity extends AppCompatActivity {

    private CameraView cv;

    private static final int SELECT_PHOTO = 0x0034;

    @SuppressLint("CheckResult")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //窗口去掉标题
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        //窗口设置为全屏
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
        //设置窗口为半透明
        getWindow().setFormat(PixelFormat.TRANSLUCENT);


        final RxPermissions rxPermissions = new RxPermissions(this);


        //提供一个帧布局
        FrameLayout fl = new FrameLayout(this);

        //创建一个照相预览用的SurfaceView子类，并放在帧布局的底层
        cv = new CameraView(this);
        fl.addView(cv);

        //防止布局
        LinearLayout linearLayout=new LinearLayout(this);
        linearLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        linearLayout.setOrientation(LinearLayout.VERTICAL);

        //创建按钮
        Button captureBtn=new Button(this);
        LinearLayout.LayoutParams params=new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        captureBtn.setLayoutParams(params);
        captureBtn.setText("拍照");
        captureBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cv.getCamera()
                        .takePicture(null, null, new CameraPictureCallback(MainActivity.this));
            }
        });

        Button albumBtn=new Button(this);
        albumBtn.setLayoutParams(params);
        albumBtn.setText("相册");
        albumBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jumpToAlbum();
            }
        });

        Button lightBtn=new Button(this);
        lightBtn.setLayoutParams(params);
        lightBtn.setText("闪光灯");
        lightBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                cv.changeFlashLight();
            }
        });

        linearLayout.addView(captureBtn);
        linearLayout.addView(albumBtn);
        linearLayout.addView(lightBtn);

        fl.addView(linearLayout);

        //设置Activity的根内容视图
        setContentView(fl);

        rxPermissions.request(Manifest.permission.CAMERA,
                Manifest.permission.WRITE_EXTERNAL_STORAGE)
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {

                    }
                });
    }

    /**
     * 跳转到相册
     */
    public void jumpToAlbum() {
        Intent intent = new Intent("android.intent.action.GET_CONTENT");
        intent.setType("image/*");
        startActivityForResult(intent, SELECT_PHOTO);
    }
}
